
using TMPro;
using UnityEngine;
using UnityEngine.UI;
// 账目查询
public class ZhangmuSet : MonoBehaviour
{

    [SerializeField] private SetSelect setSelect;
    [SerializeField] private Button[] buttons;
    private int index;
    public bool set;

    private int coinCount;//投币总数
    private int coinCountForever;//永久投币总数
    private int giftCount;//礼品总数
    private int giftCountForever;//永久礼品总数

    [SerializeField] private TextMeshProUGUI ccoinCountText;
    [SerializeField] private TextMeshProUGUI coinCountForeverText;
    [SerializeField] private TextMeshProUGUI giftCountText;
    [SerializeField] private TextMeshProUGUI giftCountForeverText;

    [SerializeField] private GameObject Tooptip;

    [SerializeField] private TextMeshProUGUI title;
    [SerializeField] private TextMeshProUGUI ccoinCountTextTiTle;
    [SerializeField] private TextMeshProUGUI coinCountForeverTextTiTle;
    [SerializeField] private TextMeshProUGUI giftCountTextTiTle;
    [SerializeField] private TextMeshProUGUI giftCountForeverTiTle;
    [SerializeField] private TextMeshProUGUI cleanTitle;
    [SerializeField] private TextMeshProUGUI exit;

    void Start()
    {

    }

    private void OnEnable()
    {
        GetData();
    }

    void Update()
    {
        if (!set)
        {
            ZhangmuSelect();
        }
        ZhangmuConfirm();
        SetText();
    }

    private void ZhangmuConfirm()
    {
        buttons[index].Select();
        if (Tooptip.activeSelf) return;
        if (Input.GetKeyDown(KeyCode.J) || ComTrans.Instance.GetsecSerial() || ComTrans.Instance.GetsetSerial())
        {
            if (!set)
            {
                Tooptip.SetActive(true);
                set = true;
                switch (index)
                {
                    case 0:
                        if (setSelect.language == 0)
                        {
                            Tooptip.GetComponent<ToolTip>().SetupToolTipText("是否清零", "ZhangmuCleanData");
                        }
                        else
                        {
                            Tooptip.GetComponent<ToolTip>().SetupToolTipText("Is it reset to zero", "ZhangmuCleanData");
                        }
                        break;
                    case 1:
                        if (setSelect.language == 0)
                        {
                            Tooptip.GetComponent<ToolTip>().SetupToolTipText("是否退出", "ZhangmuDontSave");
                        }
                        else
                        {
                            Tooptip.GetComponent<ToolTip>().SetupToolTipText("Do you want to exit", "ZhangmuDontSave");
                        }
                        break;
                }
                return;
            }
            else
            {
                set = false;
            }
        }
    }

    // 选择修改
    private void ZhangmuSelect()
    {
        if (Tooptip.activeSelf) return;
        if (Input.GetKeyDown(KeyCode.Q) || ComTrans.Instance.GetHorizontalL() == -1)
        {
            if (--index < 0) index = buttons.Length - 1;
        }
        if (Input.GetKeyDown(KeyCode.E) || ComTrans.Instance.GetHorizontalR() == 1)
        {
            if (++index >= buttons.Length) index = 0;
        }

    }

    private void SetLanguageText()
    {
        if (setSelect.language == 0)
        {
            title.text = "账目查询";
            ccoinCountTextTiTle.text = "当前投币";
            coinCountForeverTextTiTle.text = "永久投币";
            giftCountTextTiTle.text = "当前出礼";
            giftCountForeverTiTle.text = "永久出礼";
            cleanTitle.text = "清零";
            exit.text = "退出";
        }
        else if (setSelect.language == 1)
        {
            title.text = "Account inquiry";
            ccoinCountTextTiTle.text = "Current coin insertion";
            coinCountForeverTextTiTle.text = "Permanent coin insertion";
            giftCountTextTiTle.text = "Current gift giving";
            giftCountForeverTiTle.text = "Permanent gift giving";
            cleanTitle.text = "Zero clearing";
            exit.text = "Exit";
        }

    }
    private void SetText()
    {
        SetLanguageText();
        ccoinCountText.text = coinCount.ToString();
        coinCountForeverText.text = coinCountForever.ToString();
        giftCountText.text = giftCount.ToString();
        giftCountForeverText.text = giftCountForever.ToString();
    }

    public void GetData()
    {
        coinCount = PlayerPrefs.GetInt("coinCount", 0);
        giftCount = PlayerPrefs.GetInt("giftCount", 0);
        coinCountForever = PlayerPrefs.GetInt("coinCountForever", 0);
        giftCountForever = PlayerPrefs.GetInt("giftCountForever", 0);
    }

    public void ZhangmuCleanData()
    {
        coinCount = 0;
        giftCount = 0;
        PlayerPrefs.SetInt("coinCount", 0);
        PlayerPrefs.SetInt("giftCount", 0);
        PlayerPrefs.Save();
        set = false;
    }

    public void ZhangmuDontSave()
    {
        index = 0;
        set = false;
        setSelect.ChangeSetUI(0);
        Debug.Log("退出");
    }


}
